package cn.dankal.flyaudio.adapter;

import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import java.util.ArrayList;
import java.util.List;

import cn.dankal.basiclib.base.recyclerview.BaseRecyclerViewAdapter;
import cn.dankal.basiclib.base.recyclerview.BaseRecyclerViewHolder;
import cn.dankal.flyaudio.R;

public class InterastAdapter extends BaseRecyclerViewAdapter<String> {

    private List<String> pos = new ArrayList<>();
    private List<String> selectData = new ArrayList<>();


    public void setDefault(List<String> defaultSelect) {
        pos.clear();
        selectData.clear();
        for (String key : defaultSelect) {
            if (getDatas().contains(key)&&!selectData.contains(key)) {
                selectData.add(key);
                pos.add(getDatas().indexOf(key) + "");
            }
        }
        notifyDataSetChanged();
    }

    @Override
    protected int getLayoutResId(int viewType) {
        return R.layout.item_interast;
    }

    @Override
    protected BaseRecyclerViewHolder getViewHolder(ViewGroup parent, View rootView, int viewType) {
        return new ViewHolder(rootView);
    }

    public List<String> getSelectData() {
        return selectData;
    }

    public class ViewHolder extends BaseRecyclerViewHolder<String> {

        public ViewHolder(View itemView) {
            super(itemView);
        }

        @Override
        public void onBindData(String data, int position) {
            RelativeLayout itemRl = itemView.findViewById(R.id.item_rl);

            switch (position%5) {
                case 1:
                    itemRl.setGravity(Gravity.RIGHT);
                    break;
                case 2:
                    itemRl.setGravity(Gravity.LEFT);
                    break;
                default:
                    itemRl.setGravity(Gravity.CENTER_HORIZONTAL);
                    break;
            }
            setText(R.id.tv_title, data);
            itemView.setSelected(pos.contains(position + ""));
            setGone(R.id.iv_label, !pos.contains(position + ""));
            itemView.setOnClickListener(v -> {
                if (pos.contains(position + "")) {
                    pos.remove(position + "");
                    selectData.remove(data);
                    notifyDataSetChanged();
                } else {
                    pos.add(position + "");
                    selectData.add(data);
                    notifyDataSetChanged();
                }
            });
        }
    }
}
